/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * Search.java
 *
 * Created on Dec 20, 2008, 4:54:52 PM
 */

package GUI;

import Dictionary.Dictionary;
import functions.Dogy;
import java.sql.SQLException;
import java.util.ArrayList;
import javax.swing.table.DefaultTableModel;
import model.Doctor;

/**
 *
 * @author GiK
 */
public class Doctors extends javax.swing.JPanel {
    // @Exception handler
    private MainFrame frame;
    private int toChange=0;
    /** Creates new form Search */
    public Doctors(MainFrame mainFr) {
        initComponents();
        frame = mainFr;
        fillDocTable();
    }

    private void fillDocTable() {
        try
        {
            ArrayList<Doctor> docs = functions.Doctors.getAll();
            DefaultTableModel model = (DefaultTableModel) docTable.getModel();
            model.setRowCount(0);
            for(int i=0; i< docs.size(); i++)
            {
                model.addRow(new Object[]{docs.get(i).getId(),
                                          docs.get(i).getName(),
                                          docs.get(i),
                                          docs.get(i).getLoginName(),
                                          docs.get(i).getPass(),
                                          docs.get(i).getStatus() == 1 ? "Админ.":"Польз."});
            }
            docTable.setModel(model);
        }catch(SQLException e)
        {
            frame.SQLException(e);
        }
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        addDoctorDialog = new javax.swing.JDialog();
        jLabel1 = new javax.swing.JLabel();
        jSeparator1 = new javax.swing.JSeparator();
        nameField = new javax.swing.JTextField();
        surnameField = new javax.swing.JTextField();
        loginField = new javax.swing.JTextField();
        passField = new javax.swing.JTextField();
        docStatus = new javax.swing.JComboBox();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        addDoctorButton = new javax.swing.JButton();
        closeButton = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        docTable = new javax.swing.JTable();
        addNewButton = new javax.swing.JButton();
        changeButton = new javax.swing.JButton();
        deleteButton = new javax.swing.JButton();

        jLabel1.setFont(new java.awt.Font("Tahoma", 0, 14));
        jLabel1.setText("Добавление сотрудника");

        docStatus.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Пользователь", "Администратор" }));

        jLabel2.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        jLabel2.setText("Имя");

        jLabel3.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        jLabel3.setText("Фамилия");

        jLabel4.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        jLabel4.setText("Имя учетной записи");

        jLabel5.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        jLabel5.setText("Пароль");

        jLabel6.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        jLabel6.setText("Допуск");

        addDoctorButton.setText("Добавить");
        addDoctorButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                addDoctorButtonActionPerformed(evt);
            }
        });

        closeButton.setText("Закрыть");
        closeButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                closeButtonActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout addDoctorDialogLayout = new javax.swing.GroupLayout(addDoctorDialog.getContentPane());
        addDoctorDialog.getContentPane().setLayout(addDoctorDialogLayout);
        addDoctorDialogLayout.setHorizontalGroup(
            addDoctorDialogLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(addDoctorDialogLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(addDoctorDialogLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jSeparator1, javax.swing.GroupLayout.DEFAULT_SIZE, 321, Short.MAX_VALUE)
                    .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 298, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, addDoctorDialogLayout.createSequentialGroup()
                        .addGroup(addDoctorDialogLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jLabel3, javax.swing.GroupLayout.DEFAULT_SIZE, 118, Short.MAX_VALUE)
                            .addComponent(jLabel5, javax.swing.GroupLayout.DEFAULT_SIZE, 118, Short.MAX_VALUE)
                            .addComponent(jLabel6, javax.swing.GroupLayout.DEFAULT_SIZE, 118, Short.MAX_VALUE)
                            .addComponent(jLabel4, javax.swing.GroupLayout.DEFAULT_SIZE, 118, Short.MAX_VALUE)
                            .addGroup(addDoctorDialogLayout.createSequentialGroup()
                                .addGap(10, 10, 10)
                                .addComponent(jLabel2, javax.swing.GroupLayout.DEFAULT_SIZE, 108, Short.MAX_VALUE)))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 6, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGroup(addDoctorDialogLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(passField, javax.swing.GroupLayout.DEFAULT_SIZE, 197, Short.MAX_VALUE)
                            .addComponent(loginField, javax.swing.GroupLayout.DEFAULT_SIZE, 197, Short.MAX_VALUE)
                            .addComponent(surnameField, javax.swing.GroupLayout.DEFAULT_SIZE, 197, Short.MAX_VALUE)
                            .addComponent(nameField, javax.swing.GroupLayout.DEFAULT_SIZE, 197, Short.MAX_VALUE)
                            .addComponent(docStatus, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, addDoctorDialogLayout.createSequentialGroup()
                        .addComponent(closeButton)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 161, Short.MAX_VALUE)
                        .addComponent(addDoctorButton)))
                .addContainerGap())
        );
        addDoctorDialogLayout.setVerticalGroup(
            addDoctorDialogLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(addDoctorDialogLayout.createSequentialGroup()
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(addDoctorDialogLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(nameField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel2))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(addDoctorDialogLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(surnameField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel3))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(addDoctorDialogLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(loginField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel4))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(addDoctorDialogLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(passField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel5))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(addDoctorDialogLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(docStatus, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel6))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 25, Short.MAX_VALUE)
                .addGroup(addDoctorDialogLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(addDoctorButton)
                    .addComponent(closeButton))
                .addContainerGap())
        );

        setMaximumSize(new java.awt.Dimension(704, 418));
        setMinimumSize(new java.awt.Dimension(704, 418));
        setPreferredSize(new java.awt.Dimension(704, 418));

        docTable.setAutoCreateRowSorter(true);
        docTable.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        docTable.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                Dictionary.NOM,
                Dictionary.NAME,
                Dictionary.SURNAME,
                Dictionary.LOGIN,
                Dictionary.PASSWORD,
                Dictionary.ACCESS
            }
        ) {
            Class[] types = new Class [] {
                java.lang.Integer.class, java.lang.String.class, java.lang.Object.class, java.lang.String.class, java.lang.String.class, java.lang.String.class
            };
            boolean[] canEdit = new boolean [] {
                false, false, false, false, false, false
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        docTable.setRowHeight(23);
        jScrollPane1.setViewportView(docTable);
        docTable.getColumnModel().getColumn(0).setMinWidth(40);
        docTable.getColumnModel().getColumn(0).setPreferredWidth(40);
        docTable.getColumnModel().getColumn(0).setMaxWidth(40);

        addNewButton.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        addNewButton.setText(Dictionary.NEW);
        addNewButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                addNewButtonActionPerformed(evt);
            }
        });

        changeButton.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        changeButton.setText(Dictionary.CHANGE);
        changeButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                changeButtonActionPerformed(evt);
            }
        });

        deleteButton.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        deleteButton.setText(Dictionary.DELETE);
        deleteButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                deleteButtonActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 684, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(deleteButton)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(changeButton)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(addNewButton)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 362, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(addNewButton, javax.swing.GroupLayout.PREFERRED_SIZE, 35, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(changeButton, javax.swing.GroupLayout.PREFERRED_SIZE, 35, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(deleteButton, javax.swing.GroupLayout.PREFERRED_SIZE, 37, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(2, 2, 2))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void addDoctorButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_addDoctorButtonActionPerformed
        String name = nameField.getText(),
               surname = surnameField.getText(),
               login = loginField.getText(),
               pass = passField.getText();
       int status = docStatus.getSelectedIndex();
       try
       {
           
           if(toChange== 0)
              functions.Doctors.addDoctor(name, surname, login, pass,status);
           else
              functions.Doctors.changeDoctor(toChange,name, surname, login, pass,status);
           toChange = 0;
           fillDocTable();
           addDoctorDialog.dispose();
       }catch(SQLException e)
       {
           frame.FormException(addDoctorDialog, e.getMessage());
       }
    }//GEN-LAST:event_addDoctorButtonActionPerformed

    private void closeButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_closeButtonActionPerformed
        addDoctorDialog.dispose();
    }//GEN-LAST:event_closeButtonActionPerformed

    private void addNewButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_addNewButtonActionPerformed
        addDoctorButton.setText("Добавить");
        toChange =0;
        addDoctorDialog.pack();
        addDoctorDialog.setVisible(true);
        Dogy.placeToCenter(addDoctorDialog);
    }//GEN-LAST:event_addNewButtonActionPerformed

    private void deleteButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_deleteButtonActionPerformed

        try
        {
        DefaultTableModel tModel =
                            (DefaultTableModel) docTable.getModel();
                    for(int i=docTable.getSelectedRows().length-1; i >= 0 ; i--)
                    {
                      if(docTable.getValueAt(docTable.getSelectedRows()[i], 1) != null )
                      functions.Doctors.deleteDoctor(
                              ((Doctor)docTable.getValueAt(docTable.getSelectedRows()[i], 2)).getId());
                      tModel.removeRow(docTable.getSelectedRows()[i]);
                    }
                    docTable.setModel(tModel);
        }catch(SQLException e)
        {
            frame.SQLException(e);
        }
    }//GEN-LAST:event_deleteButtonActionPerformed

    private void changeButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_changeButtonActionPerformed

        if(docTable.getSelectedRowCount() > 1)
        {
          frame.FormException(this, "Выделите одного сотрудника");
          return;
        }
        addDoctorButton.setText("Изменить");
        Doctor doc = (Doctor)docTable.getValueAt(docTable.getSelectedRow(),2);
        nameField.setText(doc.getName());
        surnameField.setText(doc.getSurname());
        loginField.setText(doc.getLoginName());
        passField.setText(doc.getPass());
        docStatus.setSelectedIndex(doc.getStatus());
        toChange = doc.getId();
        addDoctorDialog.pack();
        
        addDoctorDialog.setVisible(true);
        Dogy.placeToCenter(addDoctorDialog);
    }//GEN-LAST:event_changeButtonActionPerformed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton addDoctorButton;
    private javax.swing.JDialog addDoctorDialog;
    private javax.swing.JButton addNewButton;
    private javax.swing.JButton changeButton;
    private javax.swing.JButton closeButton;
    private javax.swing.JButton deleteButton;
    private javax.swing.JComboBox docStatus;
    private javax.swing.JTable docTable;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JTextField loginField;
    private javax.swing.JTextField nameField;
    private javax.swing.JTextField passField;
    private javax.swing.JTextField surnameField;
    // End of variables declaration//GEN-END:variables

}
